package com.lwc.cfdns.controller.admin.system;

import com.lwc.cfdns.constants.ConstantsUrl;
import com.lwc.cfdns.enums.StatusEnum;
import com.lwc.cfdns.pojo.bo.system.DeptBO;
import com.lwc.cfdns.pojo.dto.ValidGroup;
import com.lwc.cfdns.pojo.dto.system.DeptDTO;
import com.lwc.cfdns.pojo.vo.system.DeptTreeVO;
import com.lwc.cfdns.pojo.vo.system.DeptVO;
import com.lwc.cfdns.service.impl.SystemDeptServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 * 部门
 *
 * @author luwc
 * @title DeptController
 * @description
 * @time 2023/5/24 17:11:12
 * @version: 1.0
 */
@RestController
@RequestMapping(ConstantsUrl.ADMIN + "/system")
public class DeptController {
    @Autowired
    private SystemDeptServiceImpl systemDeptService;

    /**
     * 创建
     *
     * @param deptDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/24 17:19:08
     */
    @PreAuthorize("hasAnyAuthority('system:dept:create')")
    @PostMapping("/dept")
    public Map<String, Object> create(@Validated(ValidGroup.Create.class) @RequestBody DeptDTO deptDTO) {
        return systemDeptService.create(deptDTO);
    }

    /**
     * 详情
     *
     * @param id
     * @return DeptVO
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/24 17:19:17
     */
    @PreAuthorize("hasAnyAuthority('system:dept:view')")
    @GetMapping("/dept/{id}")
    public DeptVO view(@PathVariable("id") @NotBlank(message = "ID不能为空") Long id) {
        return systemDeptService.view(id);
    }

    /**
     * 修改
     *
     * @param deptDTO
     * @param id
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/24 17:23:15
     */
    @PreAuthorize("hasAnyAuthority('system:dept:update')")
    @PutMapping("/dept/{id}")
    public Map<String, Object> update(@Validated(ValidGroup.Update.class) @RequestBody DeptDTO deptDTO, @PathVariable("id") @NotBlank(message = "ID不能为空") Long id) {
        return systemDeptService.update(id, deptDTO);
    }

    /**
     * 删除
     *
     * @param id
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/24 17:25:19
     */
    @PreAuthorize("hasAnyAuthority('system:dept:delete')")
    @DeleteMapping("/dept/{id}")
    public Map<String, Object> delete(@PathVariable("id") @NotBlank(message = "ID不能为空") Long id) {
        return systemDeptService.delete(id);
    }

    /**
     * 列表
     *
     * @param deptBo
     * @return List<DeptVO>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/24 17:43:02
     */
    @PreAuthorize("hasAnyAuthority('system:dept:lists')")
    @GetMapping("/dept")
    public List<DeptVO> lists(DeptBO deptBo) {
        return systemDeptService.lists(deptBo);
    }

    /**
     * 树
     *
     * @param deptBO
     * @return List<DeptTreeVO>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/26 14:58:42
     */
    @GetMapping("/dept/tree")
    public List<DeptTreeVO> tree(DeptBO deptBO) {
        deptBO.setStatus(StatusEnum.NORMAL.getCode());
        return systemDeptService.tree(deptBO);
    }
}
